package com.zqwzd.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.zqwzd.entity.User;
import com.zqwzd.service.IUserService;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IUserService userService;

	private User user = new User();
	
	private int start,limit;

	// 查询单个示例
	public String login() {
		User loginUser = userService.login(user);
		if (loginUser == null)
			System.out.println("用户名密码错误");
		else
			System.out.println("用户名:" + loginUser.getUserName() + "密码:"
					+ loginUser.getUserPass() + "性别:" + loginUser.getSex());
		return SUCCESS;
	}

	// 增加
	public String addUser() {
		try {
			userService.addUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	// 删除
	public String delUser() {
		try {
			userService.delUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	// 查询全部
	public String search() {
		try {
			List<User> users = userService.getAllUser();
			if (users.size() != 0) {
				for (User user : users) {
					System.out.println("用户名:" + user.getUserName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	// 查询全部
	public String queryPage() {
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			param.put("start",start);
			param.put("limit",limit);
			param.put("sex","男");
			List<User> users = userService.queryPage(param);
			if (users.size() != 0) {
				for (User user : users) {
					System.out.println("用户名:" + user.getUserName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
}
